package com.jinsp.startup_library;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by luoshuai on 2/25/22
 * <p>
 * 保存启动任务返回值
 */
public class StartupResultManager {

    private static volatile StartupResultManager INSTANCE;
    private final ConcurrentHashMap<Class<? extends Startup<?>>, StartupResult<?>> mStartupInitResultMap;

    private StartupResultManager() {
        mStartupInitResultMap = new ConcurrentHashMap<>();
    }

    public static StartupResultManager getInstance() {
        if (INSTANCE == null) {
            synchronized (StartupResultManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new StartupResultManager();
                }
            }
        }
        return INSTANCE;
    }

    public void saveStartupInitResult(Class<? extends Startup<?>> startupClass, StartupResult<?> startupResult) {
        mStartupInitResultMap.put(startupClass, startupResult);
    }

    public <T> StartupResult<T> getStartupInitResult(Class<? extends Startup<?>> startupClass) {
        return (StartupResult<T>) mStartupInitResultMap.get(startupClass);
    }

    public void remove(Class<? extends Startup<?>> startupClass) {
        mStartupInitResultMap.remove(startupClass);
    }

    public void clear() {
        mStartupInitResultMap.clear();
    }
}
